<?php

Class Controller_Login Extends Controller_Base
{
	
	private function check_user()
	{
		if(session_is_registered("neb_username")){
			header("location:/index");
			exit;
		}else{
			return 1;
		}

	}

	function index()
    {
    	$this->check_user();
    	
    	if($_POST)
    	{
    		if(isset($_POST['email']) and isset($_POST['password']))
    		{
    			$my_email = addslashes($_POST['email']);
    			$my_password = addslashes($_POST['password']);
    			$login_status = $this->registry['users']->login($my_email, $my_password);
    			if(!$login_status)
    			{
    				$this->registry['template']->set('mensagem', "Introduza as credenciais correctamente");
    			} else {
    				header("location:/");
    			}
    		} else {
    			$this->registry['template']->set('mensagem', "Introduza as credenciais correctamente");
    		}
    		
    	}
		$this->registry['template']->show('common/banner');
		$this->registry['template']->show('login/index');
		$this->registry['template']->show('common/footer');
    }
    
	function registo()
    {
    	$this->check_user();
    	
    	if(isset($_POST['dominio']))
    	{
    		$my_dominio = $_POST['dominio'] == 1 ? "@uma.pt" : "@max.uma.pt";
    	
    		if(isset($_POST['username']))
    		{
    			$my_username = addslashes($_POST['username']);
    			if(!preg_match('/[\d._]+/', $my_username))
    			{
    				$this->registry['template']->set('mensagem', 'Endereço inválido');
    			}
    		}
    		
    		$my_email = $my_username . $my_dominio;
    		
    		
    		
    		
    		$user_data = Array('p_nome' => 'Aluno', 'u_nome' => 'UMa', 'email' => $my_email, 'contacto' => '');
    		$user_info = $this->registry['users']->registar($user_data);
    		if(!$user_info)
    		{
    			$this->registry['template']->set('mensagem', 'Endereço inválido');
    		} else {
    			//header("location:/login/registo_2/" . $user_info['a_code']);
    		}
    		
    		
			
    	}
		$this->registry['template']->show('common/banner');
		$this->registry['template']->show('login/registo');
		$this->registry['template']->show('common/footer');
    }
    
    function registo_2()
    {
    	$this->check_user();
    	
    	$this->registry['template']->show('common/banner');
		$this->registry['template']->show('login/registo_2');
		$this->registry['template']->show('common/footer');
    }
    
    function activar()
    {
    	$this->check_user();
    	
    	$a_code = $this->registry['Uri']->getURIsegmento(2);
    	if(!$a_code)
    	{
    		$this->registry['template']->set('mensagem', 'Activação Inválida');
    	}
    	
    	$a_code = addslashes($a_code);
    	$user_info = $this->registry['users']->activar($a_code);
    	
    	if($user_info)
    	{
    		$this->registry['template']->set('user_info', $user_info);
    		$this->registry['template']->show('common/banner');
			$this->registry['template']->show('login/activado');
			$this->registry['template']->show('common/footer');
    	} else {
    		$this->registry['template']->show('common/banner');
			$this->registry['template']->show('login/activar');
			$this->registry['template']->show('common/footer');
    	}
    }
    
	function reset()
	{
		$this->check_user();
		
		if($_POST)
		{
			if(secureEmail($_REQUEST['email']))
			{
				$this->registry['users']->password_reset($_REQUEST['email']);
			}
			
			$this->registry['template']->show('common/banner');
			$this->registry['template']->set('passo_num', 2);
			$this->registry['template']->show('login/recuperar_passo2');
			$this->registry['template']->show('common/footer');
		} else {
			$this->registry['template']->show('common/banner');
			$this->registry['template']->set('passo_num', 1);
			$this->registry['template']->show('login/recuperar_passo1');
			$this->registry['template']->show('common/footer');
			
		}
	}
}

?>
